package com.bdqn.t350.ch09;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class ShiroTester {

    @Test
    public void testShiro() {

        //1.创建Realm(安全数据源)
        //目前时通过shiro.ini配置文件创建Realm,将来会通过数据库去配置
        IniRealm realm = new IniRealm("classpath:shiro.ini");
        //2、配置securityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);

        //3.获取操作的Subject,对它进行认证
        Subject subject = SecurityUtils.getSubject();

        //将用户名和密码封装成一个token，令牌
        UsernamePasswordToken token = new UsernamePasswordToken("admin", "1234561");
        try {
            subject.login(token);
            System.out.println("认证成功");
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

    }
}
